package water.faultHandler;


import app.nbdevices.service.IFaultService;
import app.service.DeviceManageService;
import app.service.FacilitiesManageService;
import com.mess.warningmess.Bean4WarnMessInfo;
import com.mess.warningmess.WarningMass;
import ele.entity.EleCycleReport;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
import water.entity.WaterCycleReport;
import water.mapper.WaterCycleReportMapper;

import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * 水表故障处理器
 */
@Component
public class WaterFaultHandler {

    @Autowired
    WaterCycleReportMapper waterCycleReportMapper;

    @Autowired
    @Qualifier("FaultService")
    IFaultService svcFault;

    public  void mainHandler(WaterCycleReport waterCycleReport){
        byte res=waterCycleReport.getWaterStatus().byteValue();
        ArrayList<Integer> faultTypeIds=new ArrayList<>();
        //电池欠电
        if(((res>>3)&0x01)==0){
            lowPowerHandler(waterCycleReport.getInternetDeviceId());
            faultTypeIds.add(15);
        }

      //获取最新历史数据
        List<WaterCycleReport> newWater1 = waterCycleReportMapper.getNewWater(waterCycleReport.getInternetDeviceId());
        WaterCycleReport newWater = newWater1.get(0);
        //读数变小
        if(newWater.getWaterData()>waterCycleReport.getWaterData()){
            reverse(waterCycleReport.getInternetDeviceId());
            faultTypeIds.add(18);
        }
        //单日数据变化过大
        if((waterCycleReport.getWaterData()-newWater.getWaterData())/(waterCycleReport.getDay()-newWater.getDay())>20.00){
            waterDataBug(waterCycleReport.getInternetDeviceId());
            faultTypeIds.add(19);
        }
        svcFault.updateEventLog(waterCycleReport.getInternetDeviceId(),faultTypeIds,"1");
    }

    public static void main(String[] args) {

        Bean4WarnMessInfo info=new Bean4WarnMessInfo();
        info.setFalut("aaa");
        info.setID("dcdvd");
        info.setOperator("dddd");
        info.setPosition("cvdvdv");
        info.setStatus("dvd");
        info.setTel("13325874078");
        info.setType("mko");
        WarningMass.sendMess(info);
      //  WarningMass.sendMessBase(m,"13325874078");
        Integer a=207;
        byte res=a.byteValue();
        ArrayList<Integer> faultTypeIds=new ArrayList<>();
        int r= (res>>3)&0x01;
        //电池欠电
        if(((res>>3)&0x01)==0){
            faultTypeIds.add(15);
        }

    }
    public void dropLine(){
        Long second = LocalDateTime.now().toEpochSecond(ZoneOffset.of("+8"));
        List<WaterCycleReport> res=waterCycleReportMapper.selectNewData();
        for(WaterCycleReport w:res){
//            if(second-w.getReportTime()>60*1000*60*24){
         //   svcFault.saveEventLog(w.getInternetDeviceId(),7,12);
            if(second-w.getReportTime()>60*60*26){
                svcFault.saveEventLog(w.getInternetDeviceId(),7,12);
            }
        }
    }
    /**
     *电池欠电
     */
    private  int lowPowerHandler(String devId){
        return svcFault.saveEventLog(devId,7,15);
    }

    /**
     * 装反
     * @param devId
     */
    private void reverse(String devId){
        svcFault.saveEventLog(devId,7,18);
    }

    /**
     * 数据变化过大
     * @param devId
     */
    private void waterDataBug(String devId){
        svcFault.saveEventLog(devId,7,19);
    }

}
